package com.infodms.dms.dao.claim.oldPart;

import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.infodms.dms.bean.TtAsWrBackListQryBean;
import com.infodms.dms.bean.TtAsWrOldPartBackListDetailBean;
import com.infodms.dms.bean.TtAsWrOldPartDetailListBean;
import com.infodms.dms.bean.TtAsWrPartsitemApplyBean;
import com.infodms.dms.common.Constant;
import com.infodms.dms.dao.common.BaseDao;
import com.infodms.dms.po.TmBusinessParaPO;
import com.infodms.dms.po.TmDealerPO;
import com.infodms.dms.po.TmVehiclePO;
import com.infodms.dms.po.TtAsWrApplicationPO;
import com.infodms.dms.po.TtAsWrOldReturnedDetailPO;
import com.infodms.dms.po.TtAsWrOldReturnedPO;
import com.infodms.dms.po.TtAsWrPartsitemPO;
import com.infodms.dms.po.TtAsWrReturnedOrderDetailPO;
import com.infodms.dms.po.TtAsWrReturnedOrderPO;
import com.infodms.dms.util.CommonUtils;
import com.infodms.dms.util.sequenceUitl.SequenceManager;
import com.infoservice.mvc.context.RequestWrapper;
import com.infoservice.po3.bean.PO;
import com.infoservice.po3.bean.PageResult;

/**
 * 类说明：
 * 作者：  赵伦达
 */
@SuppressWarnings("unchecked")
public class ClaimBackListDao extends BaseDao{
	
	public static Logger logger = Logger.getLogger(ClaimBackListDao.class);
	private static final ClaimBackListDao dao = null;
	
	public static final ClaimBackListDao getInstance() {
	   if(dao==null) return new ClaimBackListDao();
	   return dao;
	}
	/**
	 * Function：查询所有回运单状态
	 * @param  ：	
	 * @return:	
	 * @throw：	
	 * LastUpdate：	2010-6-8
	 */
	public PageResult<TtAsWrBackListQryBean> queryClaimBackList(String sqlStr,int curPage, int pageSize){
		PageResult<TtAsWrBackListQryBean> pr = pageQuery(TtAsWrBackListQryBean.class,sqlStr,null, pageSize, curPage);
		return pr;
	}
	/**
	 * Function：新增页面查询未回运清单列表
	 * @param  ：	
	 * @return:		@param sqlStr
	 * @return:		@param curPage
	 * @return:		@param pageSize
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-6-8
	 */
	public PageResult<TtAsWrPartsitemApplyBean> queryUnBackClaimList(String sqlStr,int curPage, int pageSize) {
		PageResult<TtAsWrPartsitemApplyBean> pr = pageQuery(TtAsWrPartsitemApplyBean.class,sqlStr,null, pageSize, curPage);
		
		return pr;
	}
	public boolean isExistClaimBackId(String id){
		StringBuffer sqlStr=new StringBuffer();
		sqlStr.append("select * from TT_AS_WR_OLD_RETURNED where return_no='"+id+"'");
		List rsList=select(TtAsWrOldReturnedPO.class, sqlStr.toString(), null);
		if(rsList.size()>0){
			return true;
		}else{
			return false;
		}
	}
	public void insertClaimBackMainInfo(TtAsWrOldReturnedPO insertObj){
		insert(insertObj);
	}
	/**
	 * Function：得到索赔单总数
	 * @param  ：	
	 * @return:		@param idStr
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-6-5
	 */
	public int getClaimTotalNum(String type,String idStr){
		int retNum=0;
		StringBuffer sqlStr=new StringBuffer();
		if("part_id".equals(type)){
			sqlStr.append("select count(distinct(id)) num from tt_as_wr_partsitem where part_id in('"+idStr+"')");
		}else{
			sqlStr.append("select count(distinct(id)) num from tt_as_wr_partsitem where id in("+idStr+")");
		}
		
		List rsList=pageQuery(sqlStr.toString(), null, getFunName());
		String temp=rsList.get(0).toString();
	    temp=temp.substring(temp.indexOf("="),temp.indexOf("}")).replace("=","");
		retNum=Integer.parseInt(temp);
		return retNum;
	}
	/**
	 * Function：通过索赔回运单号获得配件项数
	 * @param  ：	
	 * @return:		@param id
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-6-7
	 */
	public int getClaimPartItemNumByClaimId(String backAppId){
		int retNum=0;
		StringBuffer sqlStr=new StringBuffer();
		sqlStr.append("select count(distinct(PART_ID)) NUM from TT_AS_WR_OLD_RETURNED_DETAIL where RETURN_ID=(");
		sqlStr.append("select id from TT_AS_WR_OLD_RETURNED where RETURN_NO='"+backAppId+"')");
		List rsList=pageQuery(sqlStr.toString(), null, getFunName());
		String temp=rsList.get(0).toString();
	    temp=temp.substring(temp.indexOf("="),temp.indexOf("}")).replace("=","");
		retNum=Integer.parseInt(temp);
		
		return retNum;
	}
	/**
	 * Function：通过索赔回运单号获得索赔申请单数
	 * @param  ：	
	 * @return:		@param backAppId
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-6-7
	 */
	public int getClaimOrdToTalNum(String backAppId){
		int retNum=0;
		StringBuffer sqlStr=new StringBuffer();
		sqlStr.append("select count(*) num from TT_AS_WR_OLD_RETURNED_DETAIL where RETURN_ID=(");
		sqlStr.append("select id from TT_AS_WR_OLD_RETURNED where RETURN_NO='"+backAppId+"')");
		List rsList=pageQuery(sqlStr.toString(), null, getFunName());
		String temp=rsList.get(0).toString();
	    temp=temp.substring(temp.indexOf("="),temp.indexOf("}")).replace("=","");
		retNum=Integer.parseInt(temp);
		
		return retNum;
	}
	/**
	 * Function：通过索赔回运单号获得配件数量
	 * @param  ：	
	 * @return:		@param backAppId
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-6-7
	 */
	public int getClaimPartNum(String backAppId){
		int retNum=0;
		StringBuffer sqlStr=new StringBuffer();
		sqlStr.append("select  sum(nvl(RETURN_AMOUNT,0)) num from TT_AS_WR_OLD_RETURNED_DETAIL where RETURN_ID=(");
		sqlStr.append("select id from TT_AS_WR_OLD_RETURNED where RETURN_NO='"+backAppId+"')");
		List rsList=pageQuery(sqlStr.toString(), null, getFunName());
		String temp=rsList.get(0).toString();
	    temp=temp.substring(temp.indexOf("="),temp.indexOf("}")).replace("=","");
	    if("".equals(temp)) return 0;
		retNum=Integer.parseInt(temp);
		
		return retNum;
	}
	/**
	 * Function：得到回运配件项数
	 * @param  ：	
	 * @return:		@param idStr
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-6-5
	 */
	public int getClaimItemTotalNum(String type,String idStr){
		int retNum=0;
		StringBuffer sqlStr=new StringBuffer();
		if("part_id".equals(type)){
			sqlStr.append("select count(distinct(part_code)) num from tt_as_wr_partsitem where part_id in('"+idStr+"')");
		}else{
			sqlStr.append("select count(distinct(part_code)) num from tt_as_wr_partsitem where id in("+idStr+")");
		}
		List rsList=pageQuery(sqlStr.toString(), null, getFunName());
		String temp=rsList.get(0).toString();
	    temp=temp.substring(temp.indexOf("="),temp.indexOf("}")).replace("=","");
		retNum=Integer.parseInt(temp);
		return retNum;
	}
	
	/**
	 * Function：根据配件编号和回运单生成编号获得已回运数量
	 * @param  ：	
	 * @return:		@param idStr
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-6-5
	 */
	public int getBackedPartNumByReturnPartId(String claimOrdId,String part_id){
		int retNum=0;
		StringBuffer sqlStr=new StringBuffer();
		sqlStr.append("select nvl(sum(RETURN_AMOUNT),0) num from TT_AS_WR_OLD_RETURNED_DETAIL ");
		sqlStr.append("where part_id="+part_id+" and return_id=(select id from TT_AS_WR_OLD_RETURNED where return_no='"+claimOrdId+"')");
		List rsList=super.pageQuery(sqlStr.toString(), null, getFunName());
	    String temp=rsList.get(0).toString();
	    temp=temp.substring(temp.indexOf("="),temp.indexOf("}")).replace("=","");
	    if("".equals(temp)) return 0;
		retNum=Integer.parseInt(temp);
		
		return retNum;
	}
	/**
	 * Function：根据配件编号获得已回运数量
	 * @param  ：	
	 * @return:		@param idStr
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-6-5
	 */
	public long getDetailMainId(String return_no,String part_id){
		long retNum=0;
		StringBuffer sqlStr=new StringBuffer();
		sqlStr.append("select id from TT_AS_WR_OLD_RETURNED_DETAIL where part_id="+part_id);
		sqlStr.append(" and claim_no='"+return_no.trim()+"'");
		List rsList=pageQuery(sqlStr.toString(), null, getFunName());
	    String temp=rsList.get(0).toString();
	    temp=temp.substring(temp.indexOf("="),temp.indexOf("}")).replace("=","");
		retNum=Long.parseLong(temp);
		
		return retNum;
	}
	/**
	 * Function：根据回运配件id获得索赔申请表信息
	 * @param  ：	
	 * @return:		@param part_id
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-6-6 赵伦达
	 */
	public TtAsWrApplicationPO getClaimApplyOrdPo(String part_id){
		StringBuffer sqlStr=new StringBuffer();
		sqlStr.append("select * from TT_AS_WR_APPLICATION");
		sqlStr.append(" where id=(select id from tt_as_wr_partsitem where part_id='"+part_id+"')");
		List temp=super.select(TtAsWrApplicationPO.class, sqlStr.toString(), null);
		
		return (TtAsWrApplicationPO)temp.get(0);
	}
	public TmVehiclePO getVehicleInfo(String vin){
		StringBuffer sqlStr=new StringBuffer();
		sqlStr.append("select * from tm_vehicle");
		sqlStr.append(" where vin='"+vin+"'");
		List temp=super.select(TmVehiclePO.class, sqlStr.toString(), null);
		if(temp==null||temp.size()<=0){
			return null;
		}
		return (TmVehiclePO)temp.get(0);
	}
	/**
	 * Function：通过配件编号获得索赔配件信息表
	 * @param  ：	
	 * @return:		@param part_id
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-6-8
	 */
	public TtAsWrPartsitemPO getClaimPartItemPo(String part_id){
		StringBuffer sqlStr=new StringBuffer();
		sqlStr.append("select * from tt_as_wr_partsitem where part_id='"+part_id+"'");
		List temp=super.select(TtAsWrPartsitemPO.class, sqlStr.toString(), null);
		
		return (TtAsWrPartsitemPO)temp.get(0);
	}
	/**
	 * Function 在索赔回运明细表是否存在该配件编号的记录
	 * @param  ：	
	 * @return:		@param part_id
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-6-8
	 */
	public boolean isExistPartitemAtBackDetail(String part_id){
		StringBuffer sqlStr=new StringBuffer();
		sqlStr.append("select * from tt_as_wr_old_returned_detail where part_id="+part_id);
		List rsList=select(TtAsWrOldReturnedPO.class, sqlStr.toString(), null);
		if(rsList.size()>0){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * Function：修改索赔回运清单
	 * @param  ：	
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-6-7
	 */
	public int updateClaimBackOrdMainInfo(String id,TtAsWrOldReturnedPO vo){
		int updateNum=0;
		TtAsWrOldReturnedPO date=new TtAsWrOldReturnedPO();
		date.setReturnNo(id);
		updateNum=super.update(date, vo);
		return updateNum;
	}
	/**
	 * Function：修改索赔回运清单明细
	 * @param  ：	
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-6-7
	 */
	public int updateClaimBackOrdDetailInfo(String id,TtAsWrOldReturnedDetailPO vo){
		int updateNum=0;
		TtAsWrOldReturnedDetailPO date=new TtAsWrOldReturnedDetailPO();
		date.setId(Long.parseLong(id));
		updateNum=super.update(date, vo);
		return updateNum;
	}
	/**
	 * Function：修改索赔配件信息表
	 * @param  ：	
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-6-7
	 */
	public int updateClaimApplyPartInfo(String partId,TtAsWrPartsitemPO vo){
		int updateNum=0;
		TtAsWrPartsitemPO date=new TtAsWrPartsitemPO();
		date.setPartId(Long.parseLong(partId));
		updateNum=super.update(date, vo);
		return updateNum;
	}
	/**
	 * Function：获得索赔配件信息表的详细信息
	 * @param  ：	
	 * @return:		@param sqlStr
	 * @return:		@param curPage
	 * @return:		@param pageSize
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-6-9
	 */
	public TtAsWrOldPartBackListDetailBean getClaimBackInfo(String sqlStr,int curPage, int pageSize){
		
		TtAsWrOldPartBackListDetailBean resultPO = null;
		PageResult<TtAsWrOldPartBackListDetailBean> pr = null;
    	pr = pageQuery(TtAsWrOldPartBackListDetailBean.class,sqlStr, null, pageSize, curPage);
    	if(pr!=null && pr.getRecords()!=null && pr.getRecords().size()>0)
    		resultPO = (TtAsWrOldPartBackListDetailBean)pr.getRecords().get(0);
    	
    	return resultPO;
    }
	/**
	 * Function：获得索赔回运清单明细信息
	 * @param  ：	
	 * @return:		@param sqlStr
	 * @return:		@param curPage
	 * @return:		@param pageSize
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-6-9
	 */
	public List<TtAsWrOldPartDetailListBean> queryClaimBackDetailList(String sqlStr,int curPage, int pageSize){
		PageResult<TtAsWrOldPartDetailListBean> pr = pageQuery(TtAsWrOldPartDetailListBean.class,sqlStr,null, pageSize, curPage);
		return pr.getRecords();
	}
	
	/**
	 * 获得索赔回运清单明细信息（不带分页查询）
	 * @param sqlStr
	 * @return
	 */
	public List<TtAsWrOldPartDetailListBean> queryClaimBackDetailList(String sqlStr){
		return this.select(TtAsWrOldPartDetailListBean.class, sqlStr, null);
	}
	
	public List<TtAsWrOldReturnedDetailPO> getOldReturnedDetailPOByReturnId(String sqlStr,int curPage, int pageSize){
		PageResult<TtAsWrOldReturnedDetailPO> pr = pageQuery(TtAsWrOldReturnedDetailPO.class,sqlStr,null, pageSize, curPage);
		return pr.getRecords();
	}
	public int getExistReturnedDetailPartNum(String part_id){
		int retNum=0;
		StringBuffer sqlStr=new StringBuffer();
		sqlStr.append("select nvl(sum(RETURN_AMOUNT),0) num from TT_AS_WR_OLD_RETURNED_DETAIL ");
		sqlStr.append("where part_id="+part_id);
		List rsList=super.pageQuery(sqlStr.toString(), null, getFunName());
	    String temp=rsList.get(0).toString();
	    temp=temp.substring(temp.indexOf("="),temp.indexOf("}")).replace("=","");
	    if("".equals(temp)) return 0;
		retNum=Integer.parseInt(temp);
		return retNum;
	}
	@Override
	protected PO wrapperPO(ResultSet rs, int idx) {
		return null;
	}
	/**
	 * Function：通过回运清单主键和索赔单号判断索赔单是否已回运
	 * @param  ：	
	 * @return:		@param return_no
	 * @return:		@param claim_no
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-6-11
	 */
	public boolean isExistSameClaimNo(String return_no,String claim_no){
		int retNum=0;
		StringBuffer sqlStr=new StringBuffer();
		sqlStr.append("select * from TT_AS_WR_OLD_RETURNED_DETAIL ");
		sqlStr.append("where return_id="+return_no+" and claim_no='"+claim_no+"'");
		List rsList=super.pageQuery(sqlStr.toString(), null, getFunName());
		if(rsList!=null) retNum=rsList.size();
		if(retNum>0){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * Function：根据索赔回运清单生成号获得回运清单主键
	 * @param  ：	
	 * @return:		@param return_no
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-6-11
	 */
	public String getReturnIdByReturnNo(String return_no){
		String retStr="";
		StringBuffer sqlStr=new StringBuffer();
		sqlStr.append("select id from TT_AS_WR_OLD_RETURNED ");
		sqlStr.append("where return_no='"+return_no+"'");
		List rsList=super.pageQuery(sqlStr.toString(), null, getFunName());
	    String temp=rsList.get(0).toString();
	    temp=temp.substring(temp.indexOf("="),temp.indexOf("}")).replace("=","");
	    retStr=temp;
	    return retStr;
	}
	/**
	 * Function：通过配件编号查找索赔配件信息
	 * @param  ：	
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-6-12
	 */
	public List getPartItemPoByPartId(String part_id){
		StringBuffer sqlStr=new StringBuffer();
		sqlStr.append("select * from tt_as_wr_partsitem tt ");
		sqlStr.append("where tt.part_id="+part_id);
		PageResult<TtAsWrPartsitemPO> pr = pageQuery(TtAsWrPartsitemPO.class,sqlStr.toString(),null, 10, 1);
		return pr.getRecords();
	}
	/**
	 * Function：常规回运方式生成回运清单
	 * @param  ：	
	 * @return:		@param params
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-7-8
	 */
	public Map addNormalBackProcess(RequestWrapper request,Map params,Long logonId,Long dealer_id){
		String company_id=ClaimTools.dealParamStr(params.get("company_id"));//公司id
		String idStr=ClaimTools.dealParamStr(params.get("idStr"));//选中的索赔表中的id
		String claimOrdId=ClaimTools.dealParamStr(params.get("claimOrdId"));//回运清单号
		String claimQryDate=ClaimTools.dealParamStr(params.get("claimQryDate"));//提报日期
		String freight_type=ClaimTools.dealParamStr(params.get("freight_type"));//货运方式
		String back_type=ClaimTools.dealParamStr(params.get("back_type"));//回运类型
		String boxTotalNum=ClaimTools.dealParamStr(params.get("boxTotalNum"));//装箱总数
		String remark=ClaimTools.dealParamStr(params.get("remark"));//装箱总数
		Map retMap=new HashMap();
		StringBuffer sqlStr=new StringBuffer();
		int updateSuccessNum=0;
		
		if(!"".equals(idStr)){
			String[] array=idStr.split(",");
			if(isExistClaimBackId(claimOrdId)){//已经存在回运清单主表，只需要向回运明细表中插入新数据
				float addPratItem=0;
				//取出回运主表的信息
				sqlStr.delete(0, sqlStr.length());
				sqlStr.append("select * from TT_AS_WR_OLD_RETURNED where return_no='"+claimOrdId+"'");
				List list=select(TtAsWrOldReturnedPO.class, sqlStr.toString(), null);
				TtAsWrOldReturnedPO returnData=(TtAsWrOldReturnedPO)list.get(0);
				//根据索赔id插入到回运明细表中
				for(int count=0;count<array.length;count++){
					TtAsWrApplicationPO applicationInfo=new TtAsWrApplicationPO();
					applicationInfo=getClaimInfo(Long.parseLong(array[count]));
					List partItemList=getClaimPartInfo(Long.parseLong(array[count]));
					if(partItemList!=null){
						for(int i=0;i<partItemList.size();i++){
							TtAsWrPartsitemPO partitemInfo=new TtAsWrPartsitemPO();
							partitemInfo=(TtAsWrPartsitemPO)partItemList.get(i);
							
							addPratItem+=partitemInfo.getBalanceQuantity();//累计配件数
							
							TtAsWrOldReturnedDetailPO insertObj=new TtAsWrOldReturnedDetailPO();
							TmVehiclePO tmVinInfo=getVehicleInfo(applicationInfo.getVin());
							insertObj.setId(Long.parseLong(SequenceManager.getSequence("")));
							insertObj.setReturnId(returnData.getId());
							insertObj.setClaimNo(applicationInfo==null?"":applicationInfo.getClaimNo());
							insertObj.setVin(applicationInfo==null?"":applicationInfo.getVin());
							insertObj.setPartId(partitemInfo.getPartId());
							insertObj.setNReturnAmount(Float.valueOf(partitemInfo.getQuantity()).intValue());
							insertObj.setReturnAmount(Float.valueOf(partitemInfo.getQuantity()).intValue());
							//insertObj.setBoxNo(request.getParamValue("boxOrd"+array[count])==null?"":request.getParamValue("boxOrd"+array[count]));
							insertObj.setSignAmount(0);
							insertObj.setProducerCode(partitemInfo.getProducerCode());//配件生产商code
							insertObj.setProducerName(partitemInfo.getProducerName());//配件生产商name
							insertObj.setCreateDate(new Date());
							insertObj.setCreateBy(logonId);
							insertObj.setProcFactory(tmVinInfo==null?"":tmVinInfo.getYieldly());//产地
							dao.insert(insertObj);
							
							//同时修改索赔配件表中的回运数量
							TtAsWrPartsitemPO updateObj=new TtAsWrPartsitemPO();
							updateObj.setPartId(partitemInfo.getPartId());
							TtAsWrPartsitemPO vo=new TtAsWrPartsitemPO();
							vo.setReturnNum(partitemInfo.getQuantity());
							vo.setUpdateBy(logonId);
							vo.setUpdateDate(new Date());
							updateSuccessNum+=update(updateObj, vo);
						}
						//插入完回运明细表后修改回运主表中的索赔单数、回运配件项数、配件数
						TtAsWrOldReturnedPO updateReturnObj=new TtAsWrOldReturnedPO();
						updateReturnObj.setId(returnData.getId());
						
						TtAsWrOldReturnedPO voReturnObj=new TtAsWrOldReturnedPO();
						if(boxTotalNum!=null&&!"".equals(boxTotalNum)){//装箱总数量
							voReturnObj.setParkageAmount(Integer.parseInt(boxTotalNum));
						}
						voReturnObj.setWrAmount(returnData.getWrAmount()+array.length);
						voReturnObj.setPartItemAmount(getClaimItemTotalNum(String.valueOf(returnData.getId()),idStr));
						voReturnObj.setPartAmount(returnData.getPartAmount()+Float.valueOf(addPratItem).intValue());
						voReturnObj.setStatus(Constant.BACK_LIST_STATUS_01);//将回运清单置为未上报状态
						voReturnObj.setUpdateBy(logonId);
						voReturnObj.setUpdateDate(new Date());
						voReturnObj.setRemark(remark);
						update(updateReturnObj, voReturnObj);
					}
				}
			}else{//不存在，需要向回运主表和明细表插入新数据
				//保存索赔回运清单表
				long mainTableId=Long.parseLong(SequenceManager.getSequence(""));
				TtAsWrOldReturnedPO mainPo=new TtAsWrOldReturnedPO();
				mainPo.setId(mainTableId);
				mainPo.setDealerId(dealer_id);
				mainPo.setReturnNo(claimOrdId);
				mainPo.setWrAmount(getClaimTotalNum("",idStr));
				mainPo.setPartItemAmount(getClaimItemTotalNum(null,idStr));
				mainPo.setPartAmount(getPartItemAmountInBackInfoTable(idStr));
				if(boxTotalNum!=null&&!"".equals(boxTotalNum)){//装箱总数量
					mainPo.setParkageAmount(Integer.parseInt(boxTotalNum));
				}
				mainPo.setTransportType(Integer.parseInt(freight_type));
				mainPo.setStatus(Constant.BACK_LIST_STATUS_01);
				mainPo.setReturnType(Integer.parseInt(back_type));
				mainPo.setWrStartDate(claimQryDate);
				mainPo.setCreateDate(new Date());
				mainPo.setCreateBy(logonId);
				mainPo.setOemCompanyId(Long.parseLong(company_id));
				mainPo.setRemark(remark);
				insert(mainPo);
				//保存回运明细信息
				float addPratItem=0;
				//根据索赔id插入到回运明细表中
				for(int count=0;count<array.length;count++){
					TtAsWrApplicationPO applicationInfo=new TtAsWrApplicationPO();
					applicationInfo=getClaimInfo(Long.parseLong(array[count]));
					List partItemList=getClaimPartInfo(Long.parseLong(array[count]));
					if(partItemList!=null){
						for(int i=0;i<partItemList.size();i++){
							TtAsWrPartsitemPO partitemInfo=new TtAsWrPartsitemPO();
							partitemInfo=(TtAsWrPartsitemPO)partItemList.get(i);
							//20100930 将需要回运数量和回运数量都修改为结算数量
							addPratItem+=partitemInfo.getBalanceQuantity();//累计配件数
							
							TtAsWrOldReturnedDetailPO insertObj=new TtAsWrOldReturnedDetailPO();
							TmVehiclePO tmVinInfo=getVehicleInfo(applicationInfo.getVin());
							insertObj.setId(Long.parseLong(SequenceManager.getSequence("")));
							insertObj.setReturnId(mainTableId);
							insertObj.setClaimNo(applicationInfo==null?"":applicationInfo.getClaimNo());
							insertObj.setVin(applicationInfo==null?"":applicationInfo.getVin());
							insertObj.setPartId(partitemInfo.getPartId());
							insertObj.setNReturnAmount(Float.valueOf(partitemInfo.getBalanceQuantity()).intValue());
							insertObj.setReturnAmount(Float.valueOf(partitemInfo.getBalanceQuantity()).intValue());
							//insertObj.setBoxNo(request.getParamValue("boxOrd"+array[count])==null?"":request.getParamValue("boxOrd"+array[count]));
							insertObj.setSignAmount(0);
							insertObj.setProducerCode(partitemInfo.getProducerCode());//配件生产商code
							insertObj.setProducerName(partitemInfo.getProducerName());//配件生产商name
							insertObj.setCreateDate(new Date());
							insertObj.setCreateBy(logonId);
							insertObj.setProcFactory(tmVinInfo==null?"":tmVinInfo.getYieldly());//产地
							insert(insertObj);
							
							//同时修改索赔配件表中的回运数量
							TtAsWrPartsitemPO updateObj=new TtAsWrPartsitemPO();
							updateObj.setPartId(partitemInfo.getPartId());
							TtAsWrPartsitemPO vo=new TtAsWrPartsitemPO();
							vo.setReturnNum(partitemInfo.getBalanceQuantity());
							vo.setUpdateBy(logonId);
							vo.setUpdateDate(new Date());
							updateSuccessNum+=update(updateObj, vo);
						}
					}
				}
			}
			retMap.put("flag",1);//成功
		}else{//无法获得选中字符串
			retMap.put("flag",0);
		}
		
		return retMap;
	}
	/**
	 * Function：根据索赔单主键获取索赔信息
	 * @param  ：	
	 * @return:		@param id
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-7-8
	 */
	public TtAsWrApplicationPO getClaimInfo(Long id){
		if(id==null||id<=0) return null;
		StringBuffer sqlStr=new StringBuffer();
		sqlStr.append("select * from Tt_As_Wr_Application where id="+id);
		List list=select(TtAsWrApplicationPO.class, sqlStr.toString(), null);
		if(list!=null&&list.size()>0){
			return (TtAsWrApplicationPO)list.get(0);
		}else{
			return null;
		}
	}
	/**
	 * Function：根据索赔单主键获取索赔配件信息
	 * @param  ：	
	 * @return:		@param id
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-7-8
	 */
	public List getClaimPartInfo(Long id){
		if(id==null||id<=0) return null;
		//StringBuffer sqlStr=new StringBuffer();
		//sqlStr.append("select * from Tt_As_Wr_Partsitem where NVL(BALANCE_QUANTITY,0)>NVL(RETURN_NUM,0) AND id="+id);
		//加入配件是否需要回运条件的判断
		StringBuilder sql= new StringBuilder();
		sql.append("SELECT *\n" );
		sql.append("  FROM TT_AS_WR_PARTSITEM A\n" );
		sql.append(" WHERE NVL(A.BALANCE_QUANTITY, 0) > NVL(A.RETURN_NUM, 0)\n" );
		sql.append("   AND EXISTS (SELECT *\n" );
		sql.append("          FROM TM_PT_PART_BASE B\n" );
		sql.append("         WHERE B.PART_CODE = A.PART_CODE\n" );
		sql.append("           AND B.IS_RETURN = "+Constant.IS_NEED_RETURN+")\n" );
		sql.append("   AND A.ID = "+id);

		List list=select(TtAsWrPartsitemPO.class, sql.toString(), null);
		if(list!=null&&list.size()>0){
			return list;
		}else{
			return null;
		}
	}
	/**
	 * Function：根据索赔单id
	 * @param  ：	
	 * @return:		@return 
	 * @throw：	
	 * LastUpdate：	2010-7-8
	 */
	public int getPartItemAmountInBackInfoTable(String idStr){
		//StringBuffer sqlStr= new StringBuffer();
		int retNum=0;
		//sqlStr.append("select sum(BALANCE_QUANTITY) num from tt_as_wr_partsitem\n" );
		//sqlStr.append("where id in("+idStr+")\n" );
		//加入是否需要回运现在
		StringBuilder sql= new StringBuilder();
		sql.append("SELECT SUM(BALANCE_QUANTITY) NUM\n" );
		sql.append("  FROM TT_AS_WR_PARTSITEM A\n" );
		sql.append(" WHERE 1 = 1\n" );
		sql.append("   AND NVL(A.BALANCE_QUANTITY, 0) > NVL(A.RETURN_NUM, 0)\n" );
		sql.append("   AND EXISTS (SELECT B.PART_ID\n" );
		sql.append("          FROM TM_PT_PART_BASE B\n" );
		sql.append("         WHERE B.PART_CODE = A.PART_CODE\n" );
		sql.append("           AND B.IS_RETURN = "+Constant.IS_NEED_RETURN+")");
		sql.append("   AND ID IN ("+idStr+")\n" );
		
		List rsList=pageQuery(sql.toString(), null, getFunName());
		String temp=rsList.get(0).toString();
	    temp=temp.substring(temp.indexOf("="),temp.indexOf("}")).replace("=","");
		retNum=Integer.parseInt(temp);
		return retNum;
	}
	/**
	 * 
	* @Title: getExportDetail 
	* @Description: TODO(查询将要导出的旧件回运列表) 
	* @param @returnId  回运单ID
	* @return List<Map<String, Object>>    返回类型 
	* @throws
	 */
	public List<Map<String, Object>> getExportDetail(Long returnId, Long dealerId) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT A.CLAIM_NO, A.VIN, B.YIELDLY, B.MODEL_CODE, B.ENGINE_NO, B.IN_MILEAGE, B.TROUBLE_DESC, \n");
		sql.append("       C.PART_CODE, C.PART_NAME, C.REMARK, TO_CHAR(D.PURCHASED_DATE, 'YYYY-MM-DD')PURCHASED_DATE, \n");
		sql.append("       E.DEALER_CODE, E.DEALER_NAME, F.DELIVERER, F.DELIVERER_PHONE, H.DC_NAME \n");
		sql.append("FROM TT_AS_WR_OLD_RETURNED_DETAIL A, TT_AS_WR_APPLICATION B, TT_AS_WR_PARTSITEM C, \n");
		sql.append(     "TM_VEHICLE D, TM_DEALER E, TT_AS_REPAIR_ORDER F, TT_PT_DC_STOCK G, TM_PT_DC H\n");
		sql.append("WHERE A.RETURN_ID = ").append(returnId).append("\n");
		sql.append("  AND A.CLAIM_NO = B.CLAIM_NO \n");
		sql.append("  AND A.PART_ID = C.PART_ID \n");
		sql.append("  AND A.VIN = D.VIN \n");
		sql.append("  AND A.VIN = F.VIN \n");
		sql.append("  AND A.PART_ID = G.PART_ID(+) \n");
		sql.append("  AND B.ID = C.ID \n");
		sql.append("  AND B.DEALER_ID = ").append(dealerId).append("\n");
		sql.append("  AND B.DEALER_ID = E.DEALER_ID");
		sql.append("  AND B.RO_NO = F.RO_NO \n");
		sql.append("  AND G.DC_ID = H.DC_ID(+) \n");
		List<Map<String, Object>> ps = pageQuery(sql.toString(), null, getFunName());
		return ps;
	}
	
	/**
	 * 根据经销商ID查询该经销商级下级经销商
	 * @param dealerId 经销商ID
	 * @return
	 */
	public List<TmDealerPO> queryDealersByParentDealerId(String dealerId){
		
		if(dealerId==null)
			return new ArrayList<TmDealerPO>();
		
		StringBuilder sql= new StringBuilder();
		sql.append("SELECT D.*\n" );
		sql.append("  FROM TM_DEALER D\n" );
		sql.append(" WHERE 1 = 1\n" );
		sql.append(" START WITH D.DEALER_ID = ?\n" );
		sql.append("CONNECT BY PRIOR D.DEALER_ID = D.PARENT_DEALER_D");
		
		List<Object> paramList = new ArrayList<Object>();
		paramList.add(dealerId);
		
		List<TmDealerPO> dealerList = this.select(TmDealerPO.class, sql.toString(), paramList);
		
		if(dealerList==null)
			dealerList = new ArrayList<TmDealerPO>();

		return dealerList;
	}
	
	/**
	 * 生成回运清单明细
	 * 注意：回运索赔单状态为"审核通过","结算支付","结算审核中"
	 * @param startDate 索赔单开始时间(上报日期)
	 * @param endDate 索赔单结束日期(上报日期)
	 * @param yieldly 产地
	 * @param userId 操作人ID
	 * @param returnId 回运清单ID
	 */
	public void createReturnOrderDetail(String startDate,String endDate,String yieldly,
			Long userId,Long returnId,Long dealerId){
		
		StringBuilder sql= new StringBuilder();
		sql.append("INSERT INTO TT_AS_WR_RETURNED_ORDER_DETAIL(ID,RETURN_ID,CLAIM_NO,VIN,PART_ID,N_RETURN_AMOUNT,\n" );
		sql.append("RETURN_AMOUNT,SIGN_AMOUNT,PRODUCER_CODE,PRODUCER_NAME,CREATE_DATE,CREATE_BY,PROC_FACTORY,CLAIM_ID,PART_CODE,PART_NAME)\n" );
		sql.append("SELECT F_GETID() ID,"+returnId+" RETURN_ID,A.CLAIM_NO,A.VIN,P.PART_ID PARTID,P.BALANCE_QUANTITY N_RETURN_AMOUNT,\n" );
		sql.append("P.BALANCE_QUANTITY RETURN_AMOUNT,0 SIGN_AMOUNT,P.DOWN_PRODUCT_CODE PRODUCER_CODE,P.DOWN_PRODUCT_NAME PRODUCER_NAME,\n" );
		sql.append("SYSDATE CREATE_DATE,"+userId+" CREATE_BY,A.YIELDLY PROC_FACTORY,P.ID CLAIM_ID,P.DOWN_PART_CODE,P.DOWN_PART_NAME\n" );
		sql.append("FROM TT_AS_WR_APPLICATION A,TT_AS_WR_PARTSITEM P,TM_PT_PART_BASE PB\n" );
		sql.append("WHERE 1=1\n" );
		sql.append("AND A.ID = P.ID\n" );
		sql.append("AND P.PART_CODE = PB.PART_CODE\n" );
		sql.append("AND PB.IS_RETURN = "+Constant.IS_NEED_RETURN+"\n" );
		sql.append("AND A.YIELDLY = ?\n");
		sql.append("AND NVL(P.BALANCE_QUANTITY,0) > NVL(P.RETURN_NUM,0)\n");
		sql.append("AND A.STATUS IN (");
		sql.append(Constant.CLAIM_APPLY_ORD_TYPE_04).append(",");
		sql.append(Constant.CLAIM_APPLY_ORD_TYPE_07).append(",");
		sql.append(Constant.CLAIM_APPLY_ORD_TYPE_08).append("");
		sql.append(")\n" );
		sql.append("AND A.DEALER_ID = ?\n");
		sql.append("AND trunc(A.AUDITING_DATE) >= TO_DATE(?,'YYYY-MM')\n" );
		sql.append("AND trunc(A.AUDITING_DATE) < ADD_MONTHS(TO_DATE(?,'YYYY-MM'),1)");
		
		System.out.println("sql++"+sql);

		List<Object> paramList = new ArrayList<Object>();
		paramList.add(yieldly);
		paramList.add(dealerId);
		paramList.add(startDate);
		paramList.add(endDate);
		
		this.update(sql.toString(), paramList);
	}
	
	/**
	 * 根据回运清单明细生成回运清单信息
	 * @param roPO
	 */
	public void createReturnOrder(TtAsWrReturnedOrderPO roPO){
		
		StringBuilder sql= new StringBuilder();
		sql.append("INSERT INTO TT_AS_WR_RETURNED_ORDER\n" );
		sql.append("  (ID,DEALER_ID,TOP_DEALER_ID,DEALER_LEVEL,\n" );
		sql.append("   RETURN_NO,WR_AMOUNT,PART_ITEM_AMOUNT,PART_AMOUNT,\n" );
		sql.append("   STATUS,WR_START_DATE,RETURN_TYPE,OEM_COMPANY_ID,\n" );
		sql.append("   YIELDLY,CREATE_DATE,CREATE_BY)\n" );
		sql.append("  SELECT '"+roPO.getId()+"' ID,\n" );
		sql.append("         '"+roPO.getDealerId()+"' DEALER_ID,\n" );
		sql.append("         '"+roPO.getTopDealerId()+"' TOP_DEALER_ID,\n" );
		sql.append("         '"+roPO.getDealerLevel()+"' DEALER_LEVEL,\n" );
		sql.append("         '"+roPO.getReturnNo()+"' RETURN_NO,\n" );
		sql.append("         CLAIMCOUNT WR_AMOUNT,\n" );
		sql.append("         PART_ITEM_COUNT PART_ITEM_AMOUNT,\n" );
		sql.append("         RETURNCOUNT PART_AMOUNT,\n" );
		sql.append("         '"+roPO.getStatus()+"' STATUS,\n" );
		sql.append("         '"+roPO.getWrStartDate()+"' WR_START_DATE,\n" );
		sql.append("         '"+roPO.getReturnType()+"' RETURN_TYPE,\n" );
		sql.append("         '"+roPO.getOemCompanyId()+"' OEM_COMPANY_ID,\n" );
		sql.append("         '"+roPO.getYieldly()+"' YIELDLY,\n" );
		sql.append("         SYSDATE,\n" );
		sql.append("         '"+roPO.getCreateBy()+"' CREATE_BY\n" );
		sql.append("    FROM (SELECT COUNT(DISTINCT A.CLAIM_NO) CLAIMCOUNT,\n" );
		sql.append("                 COUNT(DISTINCT A.PART_CODE) PART_ITEM_COUNT,\n" );
		sql.append("                 SUM(A.RETURN_AMOUNT) RETURNCOUNT\n" );
		sql.append("            FROM TT_AS_WR_RETURNED_ORDER_DETAIL A\n" );
		sql.append("           WHERE 1 = 1\n" );
		sql.append("             AND A.RETURN_ID = ?)");

		
		List<Object> paramList = new ArrayList<Object>();
		paramList.add(roPO.getId());
		
		this.update(sql.toString(), paramList);

	}
	
	/**
	 * 将配件回运数量回写到索赔单之配件中
	 * @param detailPO
	 */
	public void writeBackReturnCount(TtAsWrReturnedOrderDetailPO detailPO){
		
		StringBuilder sql= new StringBuilder();
		sql.append("UPDATE TT_AS_WR_PARTSITEM P\n" );
		sql.append("   SET P.RETURN_NUM = P.BALANCE_QUANTITY\n" );
		sql.append(" WHERE 1 = 1\n" );
		sql.append("   AND P.PART_ID IN (SELECT ROD.PART_ID\n" );
		sql.append("          FROM TT_AS_WR_RETURNED_ORDER_DETAIL ROD\n" );
		sql.append("         WHERE 1 = 1\n" );
		sql.append("           AND ROD.RETURN_ID = ?)");
		
		List<Object> paramList = new ArrayList<Object>();
		paramList.add(detailPO.getReturnId());

		this.update(sql.toString(), paramList);
	}
	
	/**
	 * 将配件回运数量回写到索赔单之配件中
	 * @param detailPO
	 */
	public void writeBackReturnCountZero(TtAsWrReturnedOrderDetailPO detailPO){
		
		StringBuilder sql= new StringBuilder();
		sql.append("UPDATE TT_AS_WR_PARTSITEM P\n" );
		sql.append("   SET P.RETURN_NUM = 0\n" );
		sql.append(" WHERE 1 = 1\n" );
		sql.append("   AND P.PART_ID IN (SELECT ROD.PART_ID\n" );
		sql.append("          FROM TT_AS_WR_RETURNED_ORDER_DETAIL ROD\n" );
		sql.append("         WHERE 1 = 1\n" );
		sql.append("           AND ROD.RETURN_ID = ?)");
		
		List<Object> paramList = new ArrayList<Object>();
		paramList.add(detailPO.getReturnId());

		this.update(sql.toString(), paramList);
	}
	
	/**
	 * 将回运清单明细写入到物流单明细中
	 * @param logisticsId
	 */
	public void writeDetailToLogisticsDetail(Long logisticsId,Long userId){
		
		StringBuilder sql= new StringBuilder();
		sql.append("INSERT INTO TT_AS_WR_OLD_RETURNED_DETAIL(ID,RETURN_ID,CLAIM_NO,VIN,PART_ID,N_RETURN_AMOUNT,\n" );
		sql.append("RETURN_AMOUNT,PRODUCER_CODE,PRODUCER_NAME,PROC_FACTORY,DEALER_RETURN_ID,CREATE_BY,CREATE_DATE,CLAIM_ID,PART_CODE,PART_NAME)\n" );
		sql.append("SELECT ID,'"+logisticsId+"' RETURN_ID,CLAIM_NO,VIN,PART_ID,N_RETURN_AMOUNT,RETURN_AMOUNT,PRODUCER_CODE,\n" );
		sql.append("PRODUCER_NAME,PROC_FACTORY,RETURN_ID DEALER_RETURN_ID,'"+userId+"' CREATE_BY,SYSDATE CREATE_DATE,CLAIM_ID,PART_CODE,PART_NAME\n" );
		sql.append("FROM TT_AS_WR_RETURNED_ORDER_DETAIL A\n" );
		sql.append("WHERE 1=1\n" );
		sql.append("AND EXISTS (SELECT B.RETURN_ID FROM TR_RETURN_LOGISTICS B\n" );
		sql.append("WHERE B.RETURN_ID = A.RETURN_ID\n" );
		sql.append("AND B.LOGICTIS_ID = "+logisticsId+")");

		this.update(sql.toString(), null);
	}
	
	public void createLogisticsOrder(TtAsWrOldReturnedPO roPO,String sendTime){
		
		StringBuilder sql= new StringBuilder();
		sql.append("INSERT INTO TT_AS_WR_OLD_RETURNED(ID,DEALER_ID,RETURN_NO,WR_AMOUNT,PART_ITEM_AMOUNT,PART_AMOUNT,\n" );
		sql.append("TRANSPORT_TYPE,PARKAGE_AMOUNT,STATUS,TRAN_NO,RETURN_TYPE,CREATE_DATE,CREATE_BY,OEM_COMPANY_ID,YIELDLY,\n" );
		sql.append("SEND_TIME)\n");
		sql.append("  SELECT '"+roPO.getId()+"' ID,\n" );
		sql.append("         '"+roPO.getDealerId()+"' DEALER_ID,\n" );
		sql.append("         '"+roPO.getReturnNo()+"' RETURN_NO,\n" );
		sql.append("         CLAIMCOUNT WR_AMOUNT,\n" );
		sql.append("         PART_ITEM_COUNT PART_ITEM_AMOUNT,\n" );
		sql.append("         RETURNCOUNT PART_AMOUNT,\n" );
		sql.append("         '"+CommonUtils.checkNull(roPO.getTransportType())+"' TRANSPORT_TYPE,\n" );
		sql.append("         '"+CommonUtils.checkNull(roPO.getParkageAmount())+"' PARKAGE_AMOUNT,\n" );
		sql.append("         '"+roPO.getStatus()+"' STATUS,\n" );
		sql.append("         '"+CommonUtils.checkNull(roPO.getTranNo())+"' TRAN_NO,\n" );
		sql.append("         '"+CommonUtils.checkNull(roPO.getReturnType())+"' RETURN_TYPE,\n" );
		sql.append("         SYSDATE CREATE_DATE,\n" );
		sql.append("         '"+roPO.getCreateBy()+"' CREATE_BY,\n" );
		sql.append("         '"+roPO.getOemCompanyId()+"' OEM_COMPANY_ID,\n" );
		sql.append("         '"+roPO.getYieldly()+"' YIELDLY,\n" );
		sql.append("         TO_DATE('"+CommonUtils.checkNull(sendTime)+"','YYYY-MM-DD') SEND_TIME\n" );
		sql.append("    FROM (SELECT COUNT(DISTINCT A.CLAIM_NO) CLAIMCOUNT,\n" );
		sql.append("                 COUNT(DISTINCT A.PART_CODE) PART_ITEM_COUNT,\n" );
		sql.append("                 SUM(A.RETURN_AMOUNT) RETURNCOUNT\n" );
		sql.append("            FROM TT_AS_WR_OLD_RETURNED_DETAIL A\n" );
		sql.append("           WHERE 1 = 1\n" );
		sql.append("             AND A.RETURN_ID = "+roPO.getId()+")");
		
		this.update(sql.toString(), null);
	}
	
	/**
	 * 根据旧件物流单号和经销商级别删除物流单
	 * 注:经销商级别为该级别
	 * @param status 状态
	 * @param logisticsId 物流单ID
	 * @param dealerLevel 经销商级别=dealerLevel
	 */
	public void writeBackReturnOrderStatus(Integer status,Long logisticsId,Integer dealerLevel){
		
		if(status==null || logisticsId==null || dealerLevel==null)
			return;
		
		StringBuilder sql= new StringBuilder();
		sql.append("UPDATE TT_AS_WR_RETURNED_ORDER A\n" );
		sql.append("SET A.STATUS = ?\n" );
		sql.append("WHERE 1=1\n" );
		sql.append("AND EXISTS (SELECT * FROM TR_RETURN_LOGISTICS B\n" );
		sql.append("WHERE B.RETURN_ID = A.ID\n" );
		sql.append("AND B.LOGICTIS_ID = ?)");
		sql.append("AND A.DEALER_LEVEL = ?");

		List<Object> paramList = new ArrayList<Object>();
		paramList.add(status);
		paramList.add(logisticsId);
		paramList.add(dealerLevel);
		
		this.update(sql.toString(), paramList);
	}
	
	/**
	 * 根据旧件物流单号和经销商级别删除物流单
	 * 注：经销商级别不为该级别
	 * @param status 状态
	 * @param logisticsId 物流单ID
	 * @param dealerLevel 经销商级别<>dealerLevel
	 */
	public void writeBackReturnOrderStatus2(Integer status,Long logisticsId,Integer dealerLevel){
		
		if(status==null || logisticsId==null || dealerLevel==null)
			return;
		
		StringBuilder sql= new StringBuilder();
		sql.append("UPDATE TT_AS_WR_RETURNED_ORDER A\n" );
		sql.append("SET A.STATUS = ?\n" );
		sql.append("WHERE 1=1\n" );
		sql.append("AND EXISTS (SELECT * FROM TR_RETURN_LOGISTICS B\n" );
		sql.append("WHERE B.RETURN_ID = A.ID\n" );
		sql.append("AND B.LOGICTIS_ID = ?)");
		sql.append("AND (A.DEALER_LEVEL > ?\n");
		sql.append("OR A.DEALER_LEVEL < ?)");

		List<Object> paramList = new ArrayList<Object>();
		paramList.add(status);
		paramList.add(logisticsId);
		paramList.add(dealerLevel);
		paramList.add(dealerLevel);
		
		this.update(sql.toString(), paramList);
	}
	
	/**
	 * Iverson add By 2010-11-22 
	 * 取出最大的时间
	 * @param dealerId
	 * @return
	 */
	public String getMaxDate(String dealerId,Integer yieldly) throws Exception{
		String time="";
		StringBuffer sqlStr=new StringBuffer();
		sqlStr.append("select subStr(a.wr_start_date,12,length(a.wr_start_date)) as wr_start_date from Tt_As_Wr_Returned_Order a where a.dealer_id =? and a.yieldly=? ");
		List parList=new ArrayList();
		parList.add(dealerId);
		parList.add(yieldly);
		List<Map<String,Object>> rsList= pageQuery(sqlStr.toString(), parList, getFunName());
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		if(rsList.size()<=0){
			TmBusinessParaPO po = new TmBusinessParaPO();
			po.setTypeCode(Constant.TYPE_CODE);
			TmBusinessParaPO poValue = (TmBusinessParaPO)this.select(po).get(0);
			time=poValue.getParaValue();
			time=time.substring(11, time.length());
		}else{
			Date date=null;
			int count=0;
			for(Map map:rsList){
				String timeStr=(String)map.get("WR_START_DATE");
				if(count>0){
					Date date2=sdf.parse(timeStr);
					if(date.before(date2)){
						date=date2;
					}
				}
				date=sdf.parse(timeStr);
				count++;
			}
			time=sdf.format(date);
		}
		return time;
	}
	/**
	 * Iverson add By 2010-11-22 
	 * 取出最大的时间
	 * @param dealerId
	 * @return
	 */
	public String getMaxDate2(String dealerId,Integer yieldly) throws Exception{
		String time="";
		StringBuffer sqlStr=new StringBuffer();
		sqlStr.append("select subStr(a.wr_start_date,12,length(a.wr_start_date)) as wr_start_date from Tt_As_Wr_Returned_Order a where a.dealer_id =? and a.yieldly=? ");
		List parList=new ArrayList();
		parList.add(dealerId);
		parList.add(yieldly);
		List<Map<String,Object>> rsList= pageQuery(sqlStr.toString(), parList, getFunName());

		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date date=null;
		int count=0;
		for(Map map:rsList){
			String timeStr=(String)map.get("WR_START_DATE");
			if(count>0){
				Date date2=sdf.parse(timeStr);
				if(date.before(date2)){
					date=date2;
				}
			}
			date=sdf.parse(timeStr);
			count++;
		}
		time=sdf.format(date);
		
		return time;
	}
	
	//Iverson add By 2010-11-23 根据Id查询装箱单号(去掉重复和空)
	public List<Map<String, Object>> getBoxNo(long id) {
		StringBuffer sql = new StringBuffer();
		sql.append("select distinct a.box_no,a.return_id as id from Tt_As_Wr_Old_Returned_Detail a where a.return_id='"+id+"' and a.is_sign=0 and a.box_no is not null order by length(box_no) asc,box_no \n");
		List<Map<String, Object>> ps = pageQuery(sql.toString(), null, getFunName());
		return ps;
	}
}
